package com.songtech.uitest.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 用于一些特殊情况下，获取spring容器里的对象。比如在非spring容器管理的类里使用spring容器里的对象。
 */
@Component
public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtil.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
    /**
     * 根据Bean名称获取实例
     * @param name
     * Bean注册名称
     * @return bean实例，注意首字母要小写
     * @throws BeansException
     */

    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }
}
